﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpAlignmentUtils.h"
#include "IModelView.h"
#include "IDocument.h"
#include "GbmpWallUtils.h"
#include "GbmpBeamUtils.h"
#include "AlignmentUtils.h"
#include "IElementTransformationComponent.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

namespace
{
    std::wstring GetErrorMessage(EnAlignmentOperationErrors errorType)
    {
        std::wstring strError;
        switch (errorType)
        {
        case EAOE_PERPENDICULAR_CANNOT_TRANSFORM:
            strError = GBMP_TR(L"垂直的几何体之间不可执行对齐操作");
            break;
        case EAOE_UNKNOW:
            strError = GBMP_TR(L"不支持的对齐方式");
            break;
        default:
            strError = GBMP_TR(L"对齐操作失败");
            break;
        }
        return strError;
    }
}

bool GbmpAlignmentUtils::AlignElement(IModelView* pModelView, const IGraphicsNodeReference& gnodeReference1, const IGraphicsNodeReference& gnodeReference2, double distance, std::wstring& strError)
{
    return AlignElement(pModelView, gnodeReference1, gnodeReference2, distance, ElementId::InvalidID, strError);
}

bool GbmpAlignmentUtils::AlignElement(IModelView* pModelView, const IGraphicsNodeReference& gnodeReference1, const IGraphicsNodeReference& gnodeReference2, double distance, const ElementId& elementIdToBeAligned, std::wstring& strError)
{
    EnAlignmentOperationErrors errorType(EAOE_UNKNOW);
    OwnerPtr<IPlane> pWorkPlance = pModelView ? pModelView->GetWorkPlane() : nullptr;
    IDocument* pDoc = pModelView ? pModelView->GetDocument() : nullptr;
    bool bOk = false;
    if (elementIdToBeAligned.IsValid())
    {
        Matrix4d alignmentMat = AlignmentUtils::ComputeMatrixForNodeReferenceAlignment(pDoc, pWorkPlance.get(), gnodeReference1, gnodeReference2, distance, errorType);
        if (errorType == EAOE_NO_ERROR)
        {
            IElement* pTargetElement = pDoc->GetElement(elementIdToBeAligned);
            if (pTargetElement && pTargetElement->GetElementTransformationComponent())
            {
                pTargetElement->GetElementTransformationComponent()->Transform(alignmentMat);
                bOk = true;
            }
        }
    }
    else
    {
        bOk = AlignmentUtils::AlignGraphicsNodeReference(pDoc, pWorkPlance.get(), gnodeReference1, gnodeReference2, distance, errorType);
    }

    if (!bOk)
    {
        strError = GetErrorMessage(errorType);
    }
    return bOk;
}

bool GbmpAlignmentUtils::AlignElementWithJoining(IModelView* pModelView, const IGraphicsNodeReference& gnodeReference1, const IGraphicsNodeReference& gnodeReference2, double distance, std::wstring& strError)
{
    return AlignElementWithJoining(pModelView, gnodeReference1, gnodeReference2, distance, ElementId::InvalidID, strError);
}

bool GbmpAlignmentUtils::AlignElementWithJoining(IModelView* pModelView, const IGraphicsNodeReference& gnodeReference1, const IGraphicsNodeReference& gnodeReference2, double distance, const ElementId& elementIdToBeAligned, std::wstring& strError)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = pModelView->GetDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    IElement* pElement2 = pDoc->GetElement(gnodeReference2.GetElementId());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement2, L"pElement2为空",L"GDMPLab",L"2024-03-30");
    if (GbmpWallUtils::IsWall(pElement2) ||
        GbmpBeamUtils::IsBeam(pElement2))
    {
        // TODO:
    }

    return AlignElement(pModelView, gnodeReference1, gnodeReference2, distance, elementIdToBeAligned, strError);
}
